package com.xuxin.baby.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.contract.ChildDataRecordContract;
import com.xuxin.baby.popup.SleepPopup;
import com.xuxin.baby.presenter.ChildDataRecordPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.DatePickerUtils;
import com.xuxin.baby.view.TimeView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.12 0012
 * 记睡眠
 */
public class ChildSleepRecordActivity extends BaseActivity implements ChildDataRecordContract.View {
    private final ChildDataRecordContract.Presenter presenter = new ChildDataRecordPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.child_sleep_timer)
    TimeView child_sleep_timer;
    @BindView(R.id.child_sleep_record)
    TextView child_sleep_record;
    @BindView(R.id.child_sleep_time_start)
    TextView child_sleep_time_start;
    @BindView(R.id.child_sleep_time_end)
    TextView child_sleep_time_end;

    private ACache aCache;
    private String tenantId;//租户id
    private String token;//授权token
    private String childId;//宝宝ID
    private int sleepTime;//睡眠时间

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_child_sleep_record);
    }

    @Override
    public void initView() {
        title_name.setText("记睡眠");
        button.setText("保存");
        title_right.setImageResource(R.mipmap.img_question);
        title_right.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {
        aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        childId = aCache.getAsString(BaseConstant.childId);
        Long time = (Long) aCache.getAsObject(BaseConstant.sleepRecord);
        if (null != time) {
            child_sleep_time_start.setText(TimeUtils.millis2String(time));
            child_sleep_record.setText("结束计时");
            child_sleep_timer.startTimer((long) time);
        } else {
            child_sleep_record.setText("开始计时");
        }
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    @OnClick({R.id.title_left, R.id.title_right, R.id.child_sleep_record,
            R.id.child_sleep_time_start, R.id.child_sleep_time_end,
            R.id.button})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.title_right) {
            showPopup();
        } else if (id == R.id.child_sleep_record) {
            String record = child_sleep_record.getText().toString();
            long time = System.currentTimeMillis();
            if ("开始计时".equals(record)) {
                aCache.put(BaseConstant.sleepRecord, time);
                child_sleep_time_start.setText(TimeUtils.millis2String(time));
                child_sleep_time_end.setText("请选择时间");
                child_sleep_record.setText("结束计时");
                child_sleep_timer.startTimer(time);
            } else {
                child_sleep_time_end.setText(TimeUtils.millis2String(time));
                child_sleep_record.setText("开始计时");
                aCache.remove(BaseConstant.sleepRecord);
                child_sleep_timer.stopTimer();
            }
        } else if (id == R.id.child_sleep_time_start) {
            DatePickerUtils.showTodayTime(context, new DatePickerUtils.onListener() {
                @Override
                public void onShow(String time) {
                    child_sleep_time_start.setText(time);
                    child_sleep_timer.stopTimer();
                    child_sleep_record.setText("开始计时");
                    aCache.remove(BaseConstant.sleepRecord);
                }
            });
        } else if (id == R.id.child_sleep_time_end) {
            DatePickerUtils.showTodayTime(this, new DatePickerUtils.onListener() {
                @Override
                public void onShow(String time) {
                    child_sleep_time_end.setText(time);
                    child_sleep_timer.stopTimer();
                    child_sleep_record.setText("开始计时");
                    aCache.remove(BaseConstant.sleepRecord);
                }
            });
        } else if (id == R.id.button) {
            String startTime = child_sleep_time_start.getText().toString();
            String endTime = child_sleep_time_end.getText().toString();
            long second = (TimeUtils.string2Millis(endTime) - TimeUtils.string2Millis(startTime)) / 1000;
            if (StringUtils.isEmpty(startTime)) {
                ToastUtils.showShort("请选择开始时间");
            } else if (StringUtils.isEmpty(endTime)) {
                ToastUtils.showShort("请选择结束时间");
            } else if (second <= 0 || second >= 43200) {
                ToastUtils.showShort("时间选择有误");
            } else {
                sleepTime = (int) (second / 60);
                if (sleepTime > 0) {
                    try {
                        popup.show();
                        handler.sendEmptyMessage(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                        dismiss();
                    }
                } else {
                    ToastUtils.showShort("睡眠时间不能小于1分钟。");
                }
            }
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.record(tenantId, token, TimeUtils.getNowString(), childId, 0, 0, 0, 0, sleepTime, "sleep");
            }
            return false;
        }
    });

    @Override
    public void onSuccess(BaseBean baseBean) {
        setResult(200);
        finish();
    }

    public void showPopup() {
        new XPopup.Builder(context)
                .dismissOnTouchOutside(true)
                .asCustom(new SleepPopup(context))
                .show();
    }
}